<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
%>
<jsp:include page="../../import.jsp" flush="true" />
<link rel="StyleSheet" href="<%=path%>/lib/dtree/dtree.css" type="text/css" />
<script type="text/javascript" src="<%=path%>/lib/dtree/dtree.js"></script>
<body>


<SCRIPT LANGUAGE="JavaScript">
<!--
$(function(){
	$("#editDiv").dialog({
		autoOpen: false,
		bgiframe: true,
		//height: 500,
		width:600,
		modal: true,
		buttons: {
			"提交": function() { 
				//$(this).dialog("close"); 
				_submit();
			}, 
			"取消": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
});

//-->
</SCRIPT>
<jsp:include page="roleCache.jsp" flush="true" />
<SELECT id="appSelect" onchange="onSelectApp(this.options[this.selectedIndex].value)">
		<option>请选择应用...</option>
	 <s:iterator value="roleList" >  
		<option value='<s:property value="index"/>'><s:property value="explain"/></option>
	 </s:iterator> 
</SELECT>

<input id="editButton" type="button" value="修改数据权限" style="display:none">
<input id="addButton" type="button"  value="增加数据权限" style="display:none">
<input id="delButton" type="button"  value="删除数据权限" style="display:none">

<table class="QueryTable">
<thead>
	<th></th>
	<th>相关数据值</th>
	<th><A HREF="javascript:goOrder('S_RID')">录入者权限</A></th>
	<th><A HREF="javascript:goOrder('S_SID')">指定权限</A></th>
</thead>
 <s:iterator value="rb.resultList" id="m" status="cust_stat">  
 <TR class='<s:if test="#cust_stat.even">anthor_tr</s:if>'>  
		<TD>
		<s:if test="#m.S_SID==null">
			<INPUT TYPE="checkbox" id="id" value="<s:property value='ID'/>">
		</s:if>
		<s:else>
			<INPUT TYPE="checkbox" id="id" value="<s:property value='ID'/>" title="修改权限" sid="<s:property value='S_SID'/>">
		</s:else>
		<s:property value='NAME'/></TD>
		<TD>
		<s:iterator value="m" id='v'>  
			<s:if test="#v.key!='ID'&&#v.key!='NAME'&&#v.key!='S_RID'&&#v.key!='S_SID'">
				{<s:property value='#v.value'/>}
			</s:if>
		</s:iterator> 
		</TD>
		<TD><SCRIPT LANGUAGE="JavaScript">document.write(toTranslate('<s:property value="S_RID"/>'));	</SCRIPT></TD>
		<TD><SCRIPT LANGUAGE="JavaScript">document.write(toTranslate('<s:property value="S_SID"/>'));	</SCRIPT></TD>
</TR>
 </s:iterator> 
</table>
<jsp:include page="../../pageination.jsp" flush="true" />

<div id="editDiv" title="数据使用者角色编辑" style="display:none">
	<span align='center' >
		
		<TABLE >
		<TR height="300px">
			<TD valign='top' >
			<FORM METHOD='POST' ACTION='<%=path%>/security/dataSecurityUpdate_<s:property value="index"/>.action' onSubmit="return checkData();">
				<INPUT TYPE="hidden" NAME="index"   id="index" value=''>
				<INPUT TYPE="hidden" NAME="param"   id="param" value=''>
				<INPUT TYPE="hidden" NAME="sIndex"   id="sIndex" value='<s:property value="index"/>'>
				<INPUT TYPE="hidden" NAME="sid"   id="sid" value=''>
			</FORM>
			<div class="dtree">
		<INPUT TYPE="checkbox" id="c_ALL" value="ALL">全部
		<SCRIPT LANGUAGE="JavaScript">
			mytree = new dTree('mytree');
			mytree.config.useCheckbox = true; 				
			<s:iterator value="roles.resultList" id="m" status="cust_stat">    
				 mytree.add(<s:property value="ID"/>,<s:property value="PID"/>,'<s:property value="NAME"/>');	
			</s:iterator> 
			document.write(mytree);
		</SCRIPT>
		</div>

			</TD>
		</TR>
		</TABLE>			
	
	</span>
</div>
<!--  <TEXTAREA id="debug" ROWS="50" COLS="100"></TEXTAREA><button value="Debug" onclick="$('#debug').val($('.dtree').html());"/> -->
</body>
<SCRIPT LANGUAGE="JavaScript">
var onSelectApp  = function(index){
	if(index == null || index==''){
		return;
	}
	location.href = "dataSercurityMaintainList.action?index="+index;
}

$('#appSelect').val('<s:property value="index"/>');

var clickCache = {};
var dataIdParam = [];
$('.QueryTable :checkbox').click(function(){
	if(this.checked){
		if(this.title !=null&&this.title.length>1){
			clickCache[this.value] = "1";
		}else{
			clickCache[this.value] = "0";
		}
	}else{
		clickCache[this.value] = null;
	}
	var isAdd = false;
	var isEdit = false;
	dataIdParam = [];
	for(each in clickCache ){
		if(clickCache[each]!=null){
			if(!isEdit&&clickCache[each]=='1'){
				isEdit = true;
			}
			if(!isAdd&&clickCache[each]=='0'){
					isAdd = true;
			}
			dataIdParam.push(each);
		}
	}
	if(isEdit&&!isAdd){
		$('#editButton').show();
		$('#delButton').show();
	}else{
		$('#delButton').hide();
		$('#editButton').hide();
	}
	if(isAdd&&!isEdit){
		$('#addButton').show();
	}else{
		$('#addButton').hide();
	}
	if((isEdit&&isAdd)||(!isEdit&&!isAdd)){
		$('#editButton').hide();
		$('#addButton').hide();
	}
});

$('#editButton').click(function(){
	var tmp=[];
	 $('.QueryTable :checkbox').each(function(){
		 if(this.checked){
			 tmp.push(this.sid);
		 }
	 });
	$('#param').val(dataIdParam.join(";"));
	editDataSecurity(tmp.join(";"));
});

$('#addButton').click(function(){
	$('#param').val(dataIdParam.join(";"));
	addNewDataSecurity();
});
$('#delButton').click(function(){
	var tmp  =dataIdParam.join(";");
	$('#param').val(tmp);
	$('#sid').val('delete');
	delDataSecurity(tmp);
});

var _submit = function(isCheck){
		if(isCheck==null || isCheck){
			$('#sid').val(getDtreeValues())
			if($('#sid').val() == null || $('#sid').val() == ''){
				alert('必须设定可以操作的用户角色！');
				return;
			}
		}
		$('#editDiv').dialog('close');
		wait.show();
		$('form:first').submit();
}

var editDataSecurity = function(sid){	
	$('#editDiv').dialog('open');
	setDtreeValues(sid);
	$('#index').val('dataSecurityMaintainEdit');
	
}

var delDataSecurity = function(id){
	if(confirm('确定删除'+id+'?')){
		$('#index').val('dataSecurityMaintainDel');
		_submit(false);
	}
}
var addNewDataSecurity = function(){
	setDtreeValues(null);
	$('#editDiv').dialog('open');
	$('#index').val("dataSecurityMaintainInsert");
}
//=========================================================
var getDtreeValues = function(){
	var tmp=[];
	$('.dtree input[id^="c_"]').each(function(){
		if(this.checked){
			tmp.push(this.value);
		}
	});
	return tmp.join(";");
}

var setDtreeValues = function(values){
	$('.dtree input').attr("checked",false);

	if(values!= null && values!=''){
		$('.dtree input').each(function(){
			if(values.indexOf(this.value)>-1 && this.value!=''){
				$(this).attr("checked",true);
			}
		});
	}
}
</SCRIPT>
